package com.szqhyxj.youxiangju.biz.merchantHome.plan;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BasePromptFragment;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.core.TokenManager;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.equipmentActivation.equipmentCardslot.EquipmentCardSlotActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.deviceinfo.DeviceInfo;
import com.szqhyxj.youxiangju.biz.plan.PlanList;
import com.szqhyxj.youxiangju.biz.plan.PlanStatus;
import com.szqhyxj.youxiangju.biz.plan.RowItem;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class PlansFragment extends BasePromptFragment implements PlanListContract.View {

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.execution)
    RadioButton execution;
    @BindView(R.id.pause)
    RadioButton pause;
    @BindView(R.id.paln)
    RadioGroup paln;

    Unbinder unbinder;
    @BindView(R.id.history_project)
    LinearLayout historyProject;
    @BindView(R.id.fragment)
    FrameLayout fragment;
    @BindView(R.id.nettitle)
    TextView nettitle;
    @BindView(R.id.netArea)
    LinearLayout netArea;
    @BindView(R.id.neticon)
    ImageView neticon;

    private PlanListFragment pauseFragment;
    private PlanListFragment executionFragment;

    public static PlansFragment newInstance() {
        Bundle args = new Bundle();
        PlansFragment fragment = new PlansFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_plan;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    /*
        @Override
        public void setUserVisibleHint(boolean isVisibleToUser) {
            super.setUserVisibleHint(isVisibleToUser);
            if (isVisibleToUser) {

                if (UserInfoManger.INSTANCE.getUser().getActiviteStatus() == 0) {
                    netArea.setVisibility(View.GONE);
                } else {
                    netArea.setVisibility(View.VISIBLE);
                    if (DeviceInfo.Holder.getInstance().getOnlineState().equals("1")) {
                        neticon.setImageDrawable(getResources().getDrawable(R.mipmap.poswangluo));
                        nettitle.setText(DeviceInfo.Holder.getInstance().getAepnType());
                        nettitle.setTextColor(getResources().getColor(R.color.white));
                    } else {
                        neticon.setImageDrawable(getResources().getDrawable(R.mipmap.poswangluo1));
                        nettitle.setText("离线");
                        nettitle.setTextColor(getResources().getColor(R.color.texttm));
                    }
                }
            }
        }*/
    @Override
    protected void start() {
        initDatat();
        initEvent();
        deviceinit();

    }

    private void deviceinit() {
        if (!TokenManager.INSTANCE.online()) {
            enshowDeviceInfo();
        } else {
            if (UserInfoManger.INSTANCE.getUser().getActiviteStatus() == 0) {

                enshowDeviceInfo();
            } else {

                showDeviceInfo();

            }
        }
    }

    private void showDeviceInfo() {
        netArea.setVisibility(View.VISIBLE);
        if (DeviceInfo.Holder.getInstance().getOnlineState().equals("1")) {
            neticon.setImageDrawable(getResources().getDrawable(R.mipmap.poswangluo));
            nettitle.setText(DeviceInfo.Holder.getInstance().getAepnType());
            nettitle.setTextColor(getResources().getColor(R.color.white));
        } else {
            neticon.setImageDrawable(getResources().getDrawable(R.mipmap.poswangluo1));
            nettitle.setText("离线");
            nettitle.setTextColor(getResources().getColor(R.color.texttm));
        }
    }

    private void enshowDeviceInfo() {
        netArea.setVisibility(View.GONE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);


        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void initDatat() {
        executionFragment = PlanListFragment.newInstance(PlanStatus.RUNNING.getCode());
        pauseFragment = PlanListFragment.newInstance(PlanStatus.PAUSED.getCode());

        executionFragment.setSetListSize(new PlanListFragment.SetListSize() {
            @Override
            public void setListSize(int size) {

                execution.setText("执行中 (" + size + ")");
            }
        });

        pauseFragment.setSetListSize(new PlanListFragment.SetListSize() {
            @Override
            public void setListSize(int size) {

                pause.setText("暂停中 (" + size + ")");
            }
        });

        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先添加再隐藏起来
        transaction.add(R.id.fragment, pauseFragment);
        transaction.hide(pauseFragment);
        transaction.add(R.id.fragment, executionFragment);
        transaction.hide(executionFragment);
        // 默认显示第一个
        transaction.show(executionFragment);
        // 提交事务
        transaction.commitAllowingStateLoss();

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.history_project, R.id.execution, R.id.pause, R.id.netArea})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.history_project:
                PlanHistoryActivity.start(getActivity());
                break;
            case R.id.execution:
                showNormalPaln();
                break;
            case R.id.pause:
                showErrPlan();
                break;
            case R.id.netArea:
                EquipmentCardSlotActivity.start(getActivity());
                break;
        }
    }

    private void initEvent() {
        paln.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {

            }
        });
    }


    @Override
    public void showPlanList(List<RowItem> list) {
    }

    @Override
    public void showNormalPaln() {
        executionFragment.setList();
        getActivity().getSupportFragmentManager().beginTransaction().hide(pauseFragment).show(executionFragment).commit();
    }

    @Override
    public void showErrPlan() {
        pauseFragment.setList();
        getActivity().getSupportFragmentManager().beginTransaction().hide(executionFragment).show(pauseFragment).commit();

    }

    @Override
    public void showNone() {
    }

    @Override
    public void showRefresh(boolean isRefresh) {
    }
}